<?php
if (!EASYURPMI)
	die;

echo '<a name="content"></a><h2>'.$strAutomaticMediaSetup.'</h2>';

echo '<p>';

#$smartpm = !!$_GET['smartpm'];
#$custom_media = !!$_GET['custom_media'];

#if (!$manual_mirror && !$custom_media) {
	
#} else {
#	echo '<a href="'.$_SERVER['PHP_SELF'].'?language='.$language.'#content">'.$strGoBack.'</a>';
#}

#echo '</p>';

echo '<form action="'.$_SERVER['PHP_SELF'].'#content" method="GET">';

#if ($_GET['language']) /* $language is manual */
#	echo '<input type="hidden" name="language" value="'.$language.'">';

#echo '<p>';
#if ($smartpm) {
#	echo '<input type="hidden" name="manual_mirror" value="1">';
#	echo '<input type="hidden" name="custom_media" value="1">';
#} else {
#	if ($manual_mirror) {
#		echo '<input type="hidden" name="manual_mirror" value="1">';
#		echo '<a href="'.$_SERVER['PHP_SELF'].'?'.htmlentities('language='.$language.'&custom_media='.$custom_media).'#content">'.$strAutomatic.'</a>';
#	} else {
#		echo '<a href="'.$_SERVER['PHP_SELF'].'?'.htmlentities('manual_mirror=1&custom_media='.$custom_media.'&language='.$language).'#content">'.$strManual.'</a> '.$strNotRecommended;
#	}
#	echo '<br>';
#	if ($custom_media) {
#		echo '<input type="hidden" name="custom_media" value="1">';
#		echo '<a href="'.$_SERVER['PHP_SELF'].'?'.htmlentities('manual_mirror='.$manual_mirror.'&language='.$language).'#content">'.$strNormalMedias.'</a>';
#	} else {
#		echo '<a href="'.$_SERVER['PHP_SELF'].'?'.htmlentities('custom_media=1&manual_mirror='.$manual_mirror.'&language='.$language).'#content">'.$strCustomMedias.'</a> '.$strNotRecommended;
#	}
#}
echo '<p>';

#if ($manual_mirror && !$smartpm)
#echo "<p>$strManualNotice</p>";

$selected_distro = (!$manual_mirror || $_GET['version']) ? $_GET['version'] : $default_distro;
$selected_arch = (!$manual_mirror || $_GET['arch']) ? $_GET['arch'] : $default_arch;

echo '<p>';

/* We do not support auto-detect even in custom-media mode, due to PLF repository problems with mirrorlist */
if (!$manual_mirror && !$custom_media || !$_GET['version'] || !$_GET['arch'] || $_GET['change']) {
	echo '<label for="vers">'.$strVersion.' </label>';
	echo '<select id="vers" name="version">'."\n";
	if (!$manual_mirror && !$custom_media)
	//	echo '<option value="auto" '.(!isset($_GET['version']) || ($_GET['version'] == "auto") ? 'selected' : '').">$strAutoDetect</option>\n";
	uksort($distros, 'sort_version');
	foreach ($distros as $distro)
		echo "<option value=\"$distro\" " . ($selected_distro == $distro ? 'selected' : '') .">$distro</option>\n";
	echo "</select>\n";
	
	echo ', <label for="arch">'.$strArch.' </label>';
	echo '<select id="arch" name="arch">'."\n";
	if (!$manual_mirror && !$custom_media)
	//	echo '<option value="auto" '.(!isset($_GET['arch']) || ($_GET['arch'] == "auto") ? 'selected' : '').">$strAutoDetect</option>\n";
	ksort($archs);
	foreach ($archs as $arch)
		echo "<option value=\"$arch\" " . ($selected_arch == $arch ? 'selected' : '') .">$arch</option>\n";
	echo "</select>\n";
	if ($manual_mirror || $custom_media) {
		echo '<input type="submit" name="verarch" value="'.$strContinue.'">';
	} else {
		echo '</p><p style="font-size:8pt;">';
		printf($strCookerNotice, $cooker_numeric);
	}
} else {
	init_version_arch();
	echo '<input type="hidden" name="version" value="'.$version.'">';
	echo '<input type="hidden" name="arch" value="'.$arch.'">';
	echo $strVersion . ' <strong>'.$version.'</strong>, '.$strArch.' <strong>'.$arch.'</strong>, <input type="submit" name="change" value="'.$strModify.'">';
}

echo '</p>';
if (!$manual_mirror && !$custom_media || ($_GET['version'] && $_GET['arch'] && !$_GET['change'])) {
	foreach ($repo_name as $repo => $name) {
		echo '<fieldset title='.$name.'><legend>'.$name.'</legend>';
		echo '<p>'.htmlspecialchars($description[$repo]).' <a href="'.$url[$repo].'">'.$strMoreInfo.'</a></p>';
		if ($manual_mirror) {
			echo '<p>';
			echo '<label for="mirror_'.$repo.'">'.$strMirror.' </label>';
			echo '<select id="mirror_'.$repo.'" name="mirror['.$repo.']">'."\n";
			echo '<option value="auto" '.(!isset($_GET['mirror'][$repo]) || ($_GET['mirror'][$repo] == "auto") ? 'selected' : '').">".($smartpm?$strSelectMirror:$strAutoSelect)."</option>\n";
			usort($mirrors[$repo][$version][$arch], "sort_mirrors");
			foreach ($mirrors[$repo][$version][$arch] as $mirror) {
				echo '<option value="'.$mirror["short"]. '" ' . ($_GET['mirror'][$repo] == $mirror["short"] ? 'selected' : '') .">{$mirror['country']} ({$mirror['short']})</option>\n";
			}
			echo '</select>';
	
			echo '</p>';
		}
		if ($custom_media) {
			echo '<p>Выберите и (если необходимо) переименуйте нужные вам источник(и):</p>';
			echo '<table>';
			foreach ($medias[$repo] as $media => $mediainfo) {
				if ($mediainfo['arch'] && $mediainfo['arch'] != $arch)
					continue;
				if ($mediainfo['version'] && $mediainfo['version'] != $version)
					continue;
				$mediastring = str_replace(" ","_",$media);
				echo '<tr><td>';
				echo '<input type="checkbox" id="media_'.$mediastring.'" name="media_'.$mediastring.'"'.($_GET['media_'.$mediastring] ? " checked":"").'><label for="media_'.$mediastring.'"> '.$media.($mediainfo['comment'] ? ' ('.$mediainfo['comment'].')' : '');
				echo '</td><td>';
				echo '<input type="text" size=20 name="name_'.$media.'" value="'.$media.'">';
				echo '</td></tr>';
			}
			echo '</table>';
		} else {
			echo '<p>'.$strAddMedia.'<br>';
			printf('<input type="submit" name="autorepo['.$repo.']" value="'.$strAddMediaButton.'">', $name);
			echo '</p>';
		}
		echo '</fieldset>';
	}
	if (!$custom_media)
		echo '<p>'.$strBackportInfo.'</p>';
	/*echo '<fieldset title="'.$strAdvanced.'"><legend>'.$strAdvanced.'</legend><a name="advanced"></a>';
	if ($custom_media)
		echo '<p>'.($_GET['refreshcommands'] ? $strRunCommands : $strPressRefresh).'</p>';
	else
		echo '<p>'.$strAdvancedDesc.'</p>';
	echo '<p class="console">';
	foreach (array_keys($repo_name) as $repo) {
		$url = "";
		if ($manual_mirror)
			$url = get_mirror_url($repo);
		if ($custom_media) {
			if (!$url)
				$listurl = get_list_url($repo);
			foreach ($medias[$repo] as $media => $mediainfo) {
				$mediastring = str_replace(" ","_",$media);
				if (!$_GET['media_'.$mediastring])
					continue;
				$mediaurl = simplify_url(($url?$url.'/':'') . str_replace('$ARCH',$arch,$mediainfo["url"]));
				if ($smartpm) {
					if (!$url) {
						printf("# $strSmartpmMirrorError<br>", $repo_name[$repo]);
						continue;
					}
					echo "smart channel -y --add '".htmlspecialchars(str_replace("'","",$_GET['name_'.$mediastring]))."' type=urpmi baseurl='".$mediaurl."' hdlurl=media_info/hdlist.cz<br>\n";
					continue;
				}
				if ($url)
					echo "urpmi.addmedia '".htmlspecialchars(str_replace("'","",$_GET['name_'.$mediastring]))."' '".$mediaurl."'<br>\n";
				else
					echo "urpmi.addmedia --mirrorlist '$listurl' '".htmlspecialchars(str_replace("'","",$_GET['name_'.$mediastring]))."' '".$mediaurl."'<br>\n";
			}
		} else {
			if ($url)
				echo "urpmi.addmedia --distrib $url<br>\n";
			else
				echo "urpmi.addmedia --distrib --mirrorlist '" . get_list_url($repo) . "'<br>\n";
		}
	}
	echo '</p>';
	echo '<input type="submit" name="refreshcommands" value="'.$strRefresh.'"> '.$strRefreshDesc;
	echo '</fieldset>';*/
	
}
echo '</form>';


?>
